package com.service;

import com.pojo.Distribution;
import com.pojo.Inpstock;
import com.pojo.JdInventory;
import com.pojo.Order;
import com.util.MyDateUtil;
import com.vo.QueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
public class ConsoleService {
    
    @Resource
    InpstockService inpstockService;
    @Resource
    OrderService orderService;
    @Resource
    DistributionService distributionService;
    @Resource
    InventoryService inventoryService;
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public int inventorySize() {
        List<Inpstock> inpstocks = inpstockService.queryByAll();
        int sum = 0;
        for (Inpstock inpstock : inpstocks) {
            sum+=inpstock.getIn_number();
        }
        return sum;
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public int inventoryDay() {
        // QueryVo vo = new QueryVo();
        // Date date = null;
        // try {
        //     date = MyDateUtil.formatByDate(new Date());
        // } catch (ParseException e) {
        //     e.printStackTrace();
        // }
        //
        // vo.setBeginDate(date);
        // //日期+1
        // date = MyDateUtil.datePlusByOneDay(date);
        // vo.setEndDate(date);
        //
        // List<Inpstock> inpstocks = inpstockService.queryByDay(vo);
        // int sum = 0;
        // for (Inpstock inpstock : inpstocks) {
        //     sum+=inpstock.getIn_number();
        // }
        return 200;
    }
    
    //总销售
    public Integer salesSize() {
        List<Order> list = orderService.queryByType(null);
        return list.size();
    }
    
    //日销售
    public Integer salesDay() {
        Date date = null;
        try {
            date = MyDateUtil.formatByDate(new Date());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
        QueryVo vo = new QueryVo();
        vo.setBeginDate(date);
        date = MyDateUtil.datePlusByOneDay(date);
        vo.setEndDate(date);
    
        List<Distribution> distributions = distributionService.queryByDate(vo);
        return distributions.size();
    }
    
    // 总出库量
    public Integer outSize() {
        List<JdInventory> list = inventoryService.queryByType(null);
        Integer num = 0;
        for (JdInventory inventory : list) {
            num+= inventory.getI_outNum();
        }
        return num;
    }
    
    // 日出库量
    public Integer outDay() {
        return 0;
    }
    
    // 总入库量
    public Integer inpSize() {
        List<JdInventory> list = inventoryService.queryByType(null);
        Integer num = 0;
        for (JdInventory inventory : list) {
            num+= inventory.getI_InpNum();
        }
        return num;
    }
    
    // 日入库量
    public Integer inpDay() {
        
        return 0;
    }
    
    //总配送数
    public Integer deliverySize() {
        List<Distribution> list = distributionService.queryByType(null);
        return list.size();
    }
    
    //日配送数
    public Integer deliveryDay() {
        Date date = null;
        try {
            date = MyDateUtil.formatByDate(new Date());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
        QueryVo vo = new QueryVo();
        vo.setBeginDate(date);
        date = MyDateUtil.datePlusByOneDay(date);
        vo.setEndDate(date);
        
        List<Distribution> distributions = distributionService.queryByDate(vo);
        return distributions.size();
    }
}
